import axios from 'axios'

import {
  Notification
} from 'element-ui';



const instance = axios.create({
  // baseURL: "http://192.168.16.110:8080",
  baseURL: "http://116.62.204.74:8080",
  timeout: 5000,
  headers: {
    'X-Requested-With': 'XMLHttpRequest'
  },
})


// 添加请求拦截器
instance.interceptors.request.use(function (config) {
  let token = localStorage.token;
  if (token) {
    config.headers["x-token"] = token;
  }
  return config;
}, function (error) {
  return Promise.reject(error);
});

// 添加响应拦截器
instance.interceptors.response.use(function (response) {
  let result = response.data;
  if (result.code == 1) {
    return result;
  } else {
    if (result.code == -2) {
      Notification({
        title: '警告',
        message: 'token失效,请重新登陆',
        type: 'warning'
      });
      localStorage.removeItem('token')
      localStorage.removeItem('userInfo')
      location.href = '/#/login';
    } else {
      Notification({
        title: '警告',
        message: result.msg,
        type: 'warning'
      });
    }
  }

  return result;
}, function (error) {
  return Promise.reject(error);
});


export default instance